#include "../src/parser/sql_parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Debug test for complex CREATE VIEW issues
static void test_debug_create_view(const char* sql) {
    printf("\n=== Debugging SQL: %s ===\n", sql);
    
    parser_t* parser = parser_create(sql);
    if (!parser) {
        printf("Failed to create parser\n");
        return;
    }
    
    statement_t* stmt = parser_parse_statement(parser);
    
    if (parser_has_error(parser)) {
        printf("Parser error: %s\n", parser_get_error(parser));
    } else if (stmt && stmt->type == STMT_CREATE_VIEW) {
        printf("Successfully parsed CREATE VIEW statement\n");
        printf("View name: %s\n", stmt->data.create_view.view_name ? stmt->data.create_view.view_name : "NULL");
        printf("Has select_list: %s\n", stmt->data.create_view.select_list ? "yes" : "no");
        printf("Has from_list: %s\n", stmt->data.create_view.from_list ? "yes" : "no");
        printf("Has where_clause: %s\n", stmt->data.create_view.where_clause ? "yes" : "no");
        statement_destroy(stmt);
    } else {
        printf("Failed to parse statement or wrong statement type (type=%d)\n", stmt ? stmt->type : -1);
        if (stmt) statement_destroy(stmt);
    }
    
    parser_destroy(parser);
}

int main() {
    printf("VIEW Debug Test Suite\n");
    printf("=====================\n");
    
    // Test progressively more complex SELECT statements
    test_debug_create_view("CREATE VIEW simple_view AS SELECT name FROM users");
    test_debug_create_view("CREATE VIEW two_col_view AS SELECT name, age FROM users");
    test_debug_create_view("CREATE VIEW with_where AS SELECT name FROM users WHERE age > 18");
    test_debug_create_view("CREATE VIEW with_count AS SELECT COUNT(*) FROM users");
    test_debug_create_view("CREATE VIEW with_alias AS SELECT name as user_name FROM users");
    test_debug_create_view("CREATE VIEW with_count_alias AS SELECT COUNT(*) as total FROM users");
    test_debug_create_view("CREATE VIEW complex AS SELECT department, COUNT(*) FROM employees GROUP BY department");
    
    // Test DROP VIEW IF EXISTS debugging
    printf("\n=== Testing DROP VIEW IF EXISTS Debug ===\n");
    const char* drop_sql = "DROP VIEW IF EXISTS user_view";
    printf("Debugging SQL: %s\n", drop_sql);
    
    parser_t* parser = parser_create(drop_sql);
    if (parser) {
        statement_t* stmt = parser_parse_statement(parser);
        if (parser_has_error(parser)) {
            printf("Parser error: %s\n", parser_get_error(parser));
        } else if (stmt && stmt->type == STMT_DROP_VIEW) {
            printf("Successfully parsed DROP VIEW statement\n");
        } else {
            printf("Failed to parse DROP VIEW statement\n");
        }
        if (stmt) statement_destroy(stmt);
        parser_destroy(parser);
    }
    
    return 0;
}